package com.hewep.kit;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class TableKit {
	
	public static String getColumns(String tableName){
		Record record = Db.findFirst("select sql from sqlite_master where name = ?", tableName);
		String sql = record.getStr("sql");
		return sql.replace("CREATE TABLE \""+tableName+"\" (", "").replace(")", "").replaceAll("\"", "")
					.replaceAll("\r|\n", "").replace(" ", "").replace("TEXT", "").replace("INTEGER", "")
					.replace("idPRIMARYKEYAUTOINCREMENT,", "").replace("COLLATENOCASE", "").replace("INT", "")
					.replace("COLLATEBINARY", "").replace(",PRIMARYKEY(orgName,outModelASC", "");
	}
	
	public static String getInsertSql(String tableName){
		String columns = getColumns(tableName);
		String[] columnsArr = columns.split(",");
		StringBuilder sql = new StringBuilder("insert into "+tableName+"("+columns+") values(");
		for (int i=0; i<columnsArr.length; i++) {
			if(i == columnsArr.length - 1){
				sql.append("?");
			}else{
				sql.append("?,");
			}
		}
		sql.append(")");
		
		return sql.toString();
		
	}
}
